package bl;

import dao.WebScannerDao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Timur Yarosh
 *         Date: 13.06.13
 *         Time: 12:15
 */
public class WebScannerService {
    private final String HOST;

    public WebScannerService(String host) {
        this.HOST = host;
    }

    public List<Map<String, Integer>> searchInternalLinksWithNestingLevelInformation() throws IOException {
        WebScannerDao dao = new WebScannerDao(HOST);
        List<String> internalLinks = dao.fetchInternalLinks();
        return searchAll(internalLinks, 0);
    }

    private List<Map<String, Integer>> searchAll(List<String> internalLinks, Integer nestingLevel) throws IOException {
        List<Map<String, Integer>> allInternalLinksWithNestingLevel = new ArrayList<Map<String, Integer>>();
        Map<String, Integer> internalLinksInThisNestingLevel = new HashMap<String, Integer>();
        for (String internalLink : internalLinks) {
            System.out.println("url = " + internalLink + "\nnesting level = " + nestingLevel);
            internalLinksInThisNestingLevel.put(internalLink, nestingLevel);
            List<String> deeperLevelInternalLinks = new WebScannerDao(internalLink).fetchInternalLinks();
            if (!internalLink.equals(this.HOST)) {
                allInternalLinksWithNestingLevel.addAll(searchAll(deeperLevelInternalLinks, nestingLevel + 1));
            }
        }
        allInternalLinksWithNestingLevel.add(internalLinksInThisNestingLevel);
        return allInternalLinksWithNestingLevel;
    }
}
